package emulator.hardware.video;

import java.util.Observable;

import emulator.GUI.display.VicDisplay;
import emulator.hardware.bus.VideoBus;

public class Vic6561ParameterCache extends Observable {
	private int background_colour;
	private int char_height;
	private int columns;
	private int video_ram_address;
	private int char_rom_address;
	private int frame_colour;
	private int colour_ram_address;
	private int rows;
	private int fourth_colour;
	private int line_start;
	private int first_line;
	private boolean colour_mode;

	public Vic6561ParameterCache() {
	}

	public int getBackgroundColour() {
		return background_colour;
	}

	private void setBackgroundColour(int background_colour) {
		this.background_colour = background_colour;
	}

	public int getCharHeight() {
		return char_height;
	}

	private void setCharHeight(int char_height) {
		this.char_height = char_height;
	}

	public int getColumns() {
		return columns;
	}

	private void setColumns(int columns) {
		this.columns = columns;
	}

	public int getVideoRamAddress() {
		return video_ram_address;
	}

	private void setVideoRamAddress(int video_ram_address) {
		this.video_ram_address = video_ram_address;
	}

	public int getCharRomAddress() {
		return char_rom_address;
	}

	private void setCharRomAddress(int char_rom_address) {
		this.char_rom_address = char_rom_address;
	}

	public int getFrameColour() {
		return frame_colour;
	}

	private void setFrameColour(int frame_colour) {
		this.frame_colour = frame_colour;
	}

	public int getColourRamAddress() {
		return colour_ram_address;
	}

	private void setColourRamAddress(int colour_ram_address) {
		this.colour_ram_address = colour_ram_address;
	}

	public int getRows() {
		return rows;
	}

	private void setRows(int rows) {
		this.rows = rows;
	}

	public int getFourthColour() {
		return fourth_colour;
	}

	private void setFourthColour(int fourth_colour) {
		this.fourth_colour = fourth_colour;
	}

	public int getLineStart() {
		return line_start;
	}

	private void setLineStart(int line_start) {
		this.line_start = line_start;
	}

	public int getFirstLine() {
		return first_line;
	}

	private void setFirstLine(int first_line) {
		this.first_line = first_line;
	}
	
	public boolean getColourMode() {
		return colour_mode;
	}
	
	private void setColourMode(boolean colour_mode) {
		this.colour_mode = colour_mode;
	}

	void update(int[] vic_registers) {
		setLineStart(Math.min((vic_registers[0] & 0x7f) * 4 - Vic6561.horizontal_blank, VicDisplay.MAX_SCREEN_WIDTH));
		setFirstLine(vic_registers[1] * 2 - Vic6561.vertical_blank);
		setColumns(Math.min(32, vic_registers[2]&0x7f));
		
		setColourRamAddress(5120 + 4 * (vic_registers[2]&0x80));
		setColourRamAddress(VideoBus.convertAddress(getColourRamAddress()));
		
		setCharHeight((vic_registers[3]&1)*8 + 8);
		setRows(Math.min(38, (vic_registers[3]>>1)&0x3f));
		
		setVideoRamAddress(((vic_registers[2] & 0x80) << 2) | 
		   ((vic_registers[5] & 0xf0) << 6));
		setVideoRamAddress(VideoBus.convertAddress(getVideoRamAddress()));
		
		setCharRomAddress((vic_registers[5]&0x0f) << 10);
		setCharRomAddress(VideoBus.convertAddress(getCharRomAddress()));
		
		setFourthColour((vic_registers[14]>>4)&0x0f);
		setFrameColour(vic_registers[15]&0x07);
		setColourMode((vic_registers[15]&0x08) != 0);
		setBackgroundColour((vic_registers[15]>>4)&0x0f);
		
		setChanged();
		notifyObservers();
	}
}
